package com.byteshake.ddoffice.ui.login

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.byteshake.common.PageRouters
import com.byteshake.ddoffice.R
import com.byteshake.common.base.BaseBindingActivity
import com.byteshake.ddoffice.databinding.ActivityLoginBinding
import com.byteshake.ddoffice.ui.home.HomeActivity
import com.byteshake.ddoffice.ui.login.vm.LoginIntent
import com.byteshake.ddoffice.ui.login.vm.LoginViewModel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.map

@Route(path = PageRouters.Login_Main)
class LoginActivity : BaseBindingActivity<ActivityLoginBinding>({
    ActivityLoginBinding.inflate(it) }) {

    private val vm: LoginViewModel = LoginViewModel()

    override fun initView(savedInstanceState: Bundle?) {

    }

    override fun initData(savedInstanceState: Bundle?) {
        Log.e(TAG, "initData: 初始化")
        binding.btnLogin.setOnClickListener {
            Log.d(TAG, "sendUiIntent: login---")
            vm.sendUiIntent(LoginIntent.Login(15112345678, "4297f44b13955235245b2497399d7a93"))
//            startActivity(Intent(this, HomeActivity::class.java))
//            ARouter.getInstance().build(PageRouters.App_Home).navigation(this)
//            finish()
        }
        lifecycleScope.launchWhenStarted {
            vm.uiStateFlow.map { it.loginLoginState }
                .collect{
                    Log.e(TAG, "login result: $it")
                    ARouter.getInstance().build(PageRouters.App_Home).navigation(this@LoginActivity)
                    finish()
                }
        }
    }
}